<?php
namespace App\Http\Controllers;
/**
 * 
 * 民宿控制器
 * 
 * @author manong
 *
 */
use App\Services\BaiduMapService;
use App\Services\HotelService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class HotelController extends BaseController {

    /**
     * 获取民宿列表
     *
     * @param HotelService $hotelService 民宿服务
     * @param BaiduMapService $mapService 地图服务服务
     * @return mixed
     */
    public function getHotelsList(HotelService $hotelService,BaiduMapService $mapService){

        //参数
        //【经纬度】
        //    lat   经度
        //    lon   维度
        //【查询等级】
        //    level city or country
        //【排序】
        //  sort_type 排序方式 price or sales or distance or views
        //  sort    升降序 asc or desc
        //【位置】
        // country 0-全部 or 选择的县名
        //【翻页】
        //firsstrow
        //pagesize

        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);
//        $level =Input::get('level','city');
        $province = Input::get('province');
        $city = Input::get('city');
        $country =Input::get('district');
        if(empty($country)){
            $country =Input::get('country');
        }
        $sort_type =Input::get('sort_type','price');
        $sort =Input::get('sort','asc');
        $firstrow =Input::get('firstrow',0);
        $pagesize =Input::get('pagesize',10);
		$cityname = Input::get('cityname');
		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}
		
//        $location = $mapService->locationByGPS($lon,$lat);
//        if(!$location){
//            return $this->failure('根据定位获取城市失败。');
//        }

//        $data = $hotelService->getList($lat,$lon,$level,$location['province'],$location['city'],$country,$sort_type,$sort,$firstrow,$pagesize);
		$data = $hotelService->getList($lat,$lon,$province, $city,$country,$sort_type,$sort,$firstrow,$pagesize);
        if(!$data['result']){
            return $this->failure($data['msg']);
        }
        return $this->success($data['data']);
    }

    /**
     * 获取民宿详情
     *
     * @param $hotels_id
     * @param HotelService $hotelService
     * @return mixed
     */
    public function getHotelsDetail($hotels_id, HotelService $hotelService){

        //参数编辑
        $hotels_id = intval($hotels_id);
        $user_id = intval(Input::get('user_id',0));
        if (!$hotels_id) {
            return $this->args_invalid();
        }

        //main
        $data = $hotelService->getHotelsDetail($hotels_id,$user_id);
        if ($data['hotels_info']['base']['tags'])
        {
           $data['hotels_info']['base']['tags'] = trim(str_replace(',',' ', $data['hotels_info']['base']['tags']));
           $data['hotels_info']['base']['tags'] = trim(str_replace('，',' ', $data['hotels_info']['base']['tags']));

        }

        if($data===false){
            return $this->failure('该民宿不存在');
        }

        /* 更新浏览次数 */
        $hotelService->update_statistics($hotels_id,'views');

        //编辑结果
        return $this->success($data);
    }

//    /**
//     * 获取线路的评价列表
//     *
//     * @param $tour_id
//     * @param TourService $tourService 线路服务
//     * @return mixed
//     */
//    public function getComments($tour_id,TourService $tourService){
//
//        $firstrow =Input::get('firstrow');
//        $pagesize =Input::get('pagesize');
//
//        if(!$tour_id){
//            return $this->args_invalid();
//        }
//
//        $data = $tourService->getComments($tour_id,$firstrow,$pagesize);
//        if(!$data['result']){
//            return $this->failure($data['msg']);
//        }
//        return $this->success($data['data']);
//
//    }
//
}
